package com.wf.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wf.constant.RedisConstant;
import com.wf.context.BaseContext;
import com.wf.domain.EnterpriseInfo;
import com.wf.domain.PaymentRecord;
import com.wf.domain.User;
import com.wf.exception.ApartmentException;
import com.wf.mapper.EnterpriseInfoMapper;
import com.wf.mapper.PaymentRecordMapper;
import com.wf.mapper.UserMapper;
import com.wf.service.AlipayService;
import com.wf.service.EnterpriseInfoService;
import com.wf.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;

/**
* @author mpk16
* @description 针对表【enterprise_info】的数据库操作Service实现
* @createDate 2024-08-03 13:39:49
*/
@Service
public class EnterpriseInfoServiceImpl extends ServiceImpl<EnterpriseInfoMapper, EnterpriseInfo>
    implements EnterpriseInfoService{

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    EnterpriseInfoMapper enterpriseInfoMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    PaymentRecordMapper paymentRecordMapper;
    @Autowired
    AlipayService alipayService;
    @Override
    public void setInvoice(String manageId) throws ApartmentException {

        Long userId = BaseContext.getCurrentId();

        LambdaUpdateWrapper<EnterpriseInfo> wrapper = new LambdaUpdateWrapper<EnterpriseInfo>().eq(
                EnterpriseInfo::getUserId, userId
        );
        EnterpriseInfo enterpriseInfo = enterpriseInfoMapper.selectOne(wrapper);
        if(ObjectUtil.isEmpty(enterpriseInfo))
        {
            throw  new ApartmentException("企业信息不存在，请先完善企业信息!");
        }
        LambdaUpdateWrapper<PaymentRecord> wrapper1 = new LambdaUpdateWrapper<PaymentRecord>().eq(
                PaymentRecord::getPaymentId, manageId
        );
        PaymentRecord record = paymentRecordMapper.selectOne(wrapper1);
        User user = userMapper.selectById(userId);

        //订单流水号
        String tradeNo = record.getTradeNo();
        //申请人
        String realName = user.getRealName();
        //报销单位名称
        String enterName = enterpriseInfo.getEnterName();
        //社会信用代码
        String enterNumber = enterpriseInfo.getEnterNumber();
        //金额
        HashMap<String, Object> map = new HashMap<>();
        map.put("trade_no", tradeNo);
        map.put("out_trade_no",manageId);
        String res = alipayService.tradeQuery(map);
        JSONObject jsonObject = JSONUtil.parseObj(res);
        JSONObject response = jsonObject.getJSONObject("alipay_trade_query_response");
        if(StrUtil.equals((String)response.get("msg"),"Business Failed"))
        {
            throw new ApartmentException("交易不存在！");
        }
        //交易总金额
        Object totalAmount = response.get("total_amount");
        //交易时间
        Object sendPayDate = response.get("send_pay_date");
        //付款账户
        Object buyerLogonId = response.get("buyer_logon_id");
        System.out.println(jsonObject);
        map.put("enterNumber", enterNumber);
        map.put("sendPayDate", sendPayDate);
        map.put("totalAmount", totalAmount);
        map.put("enterName", enterName);
        map.put("realName", realName);
        map.put("buyerLogonId",buyerLogonId);
        map.put("userId", userId);
        map.put("phone", user.getPhone());
        map.put("email", user.getEmail());

        redisUtil.sSet(RedisConstant.APARTMENT_INVOICE_APPLY , map);

    }

    @Override
    public boolean isCompany() {
        Long userId = BaseContext.getCurrentId();

        LambdaUpdateWrapper<EnterpriseInfo> wrapper = new LambdaUpdateWrapper<EnterpriseInfo>().eq(
                EnterpriseInfo::getUserId, userId
        );
        EnterpriseInfo enterpriseInfo = enterpriseInfoMapper.selectOne(wrapper);
        if(ObjectUtil.isEmpty(enterpriseInfo))
        {
            return false;
        }else
        {
            return true;
        }
    }
}




